﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace 摇奖机
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool _startFlag = false;
        Thread td;
        private void button_Start_Click(object sender, EventArgs e)
        {           
            if (_startFlag == false)
            {
                _startFlag = true;
                button_Start.Text = "停止";
                td = new Thread(PlayGame);
                td.IsBackground = true;
                td.Name = "PlayGame";
                td.Start();
            }
            else
            {
                _startFlag = false;
                button_Start.Text = "开始";               
            }

            
        }

        private void PlayGame()
        {
            Random rd = new Random();
            while (_startFlag)
            {
                label1.Text = rd.Next(1, 10).ToString();
                label2.Text = rd.Next(1, 10).ToString();
                label3.Text = rd.Next(1, 10).ToString();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (td != null)
            {
                td.Abort();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 此实验表明，即使线程执行完了所有工作，并不会Abort，更不会为空，除非被主动Abort,可能是人也可能是系统
            button1.Text = (td.ThreadState == ThreadState.Aborted) ? ("空") : ("非空");
        }
    }
}
